package com.shujia.student.mr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

public class Demo5Reduce {
    public static void main(String[] args) throws Exception {

        //获取文件列表
        File file = new File("java/data/tmp2");
        File[] files = file.listFiles();

        HashMap<String, Long> hashMap = new HashMap<String, Long>();

        for (File file1 : files) {

            FileReader fileReader = new FileReader(file1);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");
                String clazz = split[0];
                long num = Long.parseLong(split[1]);

                Long aLong = hashMap.get(clazz);
                //如果班级不存在将当前的人数存到map中
                if (aLong == null) {
                    hashMap.put(clazz, num);
                } else {
                    hashMap.put(clazz, aLong + num);
                }
            }

            bufferedReader.close();
        }


        System.out.println(hashMap);

    }
}
